package com.lzz.bottomencapsulation.app;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;

import com.lzz.bottomencapsulation.manager.AppManager;
import com.lzz.bottomencapsulation.utils.LUtils;
import com.lzz.bottomencapsulation.utils.TUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Application  基础封装----架构model内
 */
public abstract class BaseApplication extends Application {
    private  final String TAG = this.getClass().getName();
    private AppManager appManager;//activity管理
    private static BaseApplication baseApplication;

    public AppManager getAppManager() {
        return appManager;
    }

    public static BaseApplication getBaseApplication() {
        return baseApplication;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LUtils.v(TAG, "onCreate");
        baseApplication = this;
        appManager = AppManager.getAppManager();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        LUtils.v(TAG, "onTerminate程序终止的时候执行");
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        LUtils.v(TAG, "onLowMemory低内存的时候执行");
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        LUtils.v(TAG, "onTrimMemory程序在内存清理的时候执行");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LUtils.v(TAG, "onConfigurationChanged");
    }

    protected static Boolean isExit = false;
    /**
     * 双击退出函数
     */
    public void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true;
            TUtils.showShort(this, "再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);

        } else {
            getAppManager().AppExit(this);
            appExit();
        }
    }
    public abstract void appExit();
    @Override
    public Context getApplicationContext() {
        return super.getApplicationContext();
    }

}
